var app = getApp();
const $ = require('../../../utils/utils.js')
const user = require('../../../manager/userManager.js')
const config = require('../../../config.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hdCardInitiatorId: "",
        shareInfo: {},
        attendList: [],
        inviteStatus: false,
        inviteShow: false,
        initorId: "",
        content: '',
        activityId: ''
    },

    /**
 * 生命周期函数--监听页面加载
 */
    onLoad: function (options) {
        wx.hideShareMenu()
        console.log("#invitor_share_result_onShow()", options)
        var that = this;
        that.setData({
            hdCardInitiatorId: options.hdCardInitiatorId,
            activityId: options.activityId,
            usertoken: wx.getStorageSync("usertoken"),
            userid: wx.getStorageSync("userid"),
            mobile: wx.getStorageSync("userPhone")
        });
        wx.setStorageSync('initorId', options.hdCardInitiatorId);
        this.setData({
            initorId: options.hdCardInitiatorId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log("#invitor_share_result_onReady()")
    },



    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {
        console.log("#invitor_share_result_onShow()")
        
        this.getUserInfo()

    },

    //存储用户昵称、头像信息
    getUserInfo: function (cb) {
        var that = this;
        console.log("#invitor_share_result_getUserInfo()")
        wx.getUserInfo({
            withCredentials: false,
            success: function (res) {
                console.log("#invitor_share_result_getUserInfo()_success", wx.getStorageSync("usertoken"))
                wx.setStorageSync('userinfo', res.userInfo);
                if(wx.getStorageSync("usertoken")){
                    user.bindWXUserToUser(function () {
                        console.log("#invitor_share_result_getUserInfo()")
                        that.getShareInfo(that.data.initorId);
                    });
                    return
                }
                app.addListener(function (changedData) {
                    console.log("#invitor_share_result_app.addListener", changedData)
                    user.bindWXUserToUser(function () {
                        console.log("#invitor_share_result_getUserInfo()")
                        that.getShareInfo(that.data.initorId);
                    });
                });
            },

            fail: function (res) {
                wx.getSetting({
                    success: (res) => {
                        console.log('getSetting', res);
                        if (res.authSetting['scope.userInfo'] == false || res.authSetting['scope.userLocation'] == false) {
                            wx.showModal({
                                title: '提示',
                                content: '需要您设置用户授权才能体验哦~',
                                showCancel: false,
                                confirmText: '去设置',
                                success: function (opt) {
                                    wx.openSetting({
                                        success: (res) => {

                                        }
                                    });
                                }
                            });
                        }
                    },
                });
            }
        })
    },




    //活动规则 
    activityRules: function () {
        var that = this;
        that.setData({
            inviteShow: true
        });
    },

    // 关闭活动规则遮罩层
    shut_shadow: function () {
        var that = this;
        that.setData({
            inviteShow: false
        });
    },

    // 点击抢月卡
    clickActivity: function () {
        $.goto(config.page.grabCard)
    },

    // 发起活动
    startInvite: function () {
        let that = this
        if (!wx.getStorageSync("isLogin")) {
            $.goto(config.page.login + '?path=activity')
        } else {
            let pages = getCurrentPages()
            if (pages.length > 1) {
                wx.navigateBack({
                })
            } else {
                $.goto(config.page.getMonthCard + '?activityId=' + that.data.activityId)
            }


        }

    },

    // 参加活动进入小程序
    redictIndex: function () {
        wx.reLaunch({
            url: config.page.index,
        })
    },

    // 根据活动id获取分享者信息
    getShareInfo: function (hdCardInitiatorId) {
        
        var that = this;
        let data = {
            initorId: hdCardInitiatorId,
            appid: app.appId,
            usertoken: wx.getStorageSync("usertoken"),
            userid: wx.getStorageSync("userid"),
            mobile: wx.getStorageSync("userPhone")
        };
        let url = $.getUrl(config.getParentCardInfo, data)
        $.get(url).then(data => {
            console.log("#invitor_share_result_getShareInfo()",data)
            if (data.code == 0) {
                wx.setStorageSync('activityId', data.detail.initiator.activityId);
                that.setData({
                    attendList: data.detail.attendors,
                    shareInfo: data.detail.initiator,
                    content: data.detail.content
                });
            }
            //抢卡显示，当前参与者未帮忙抢 且 当前发起活动 < 100%
            if (data.detail.currentAttender == null && data.detail.initiator.finishRate < 100) {
                that.setData({
                    inviteStatus: true
                });
            } else {
                that.setData({
                    inviteStatus: false
                });
            }
        }).catch(error => {
            console.log("#invitor_share_result_getShareInfo()_error", error)
        })


    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        if (res.from === 'menu') {
            // 来自页面内转发按钮
            console.log(res.target)
            return
        }
        return {
            title: '帮我抢月卡，免费读绘本',
            imageUrl: "../../../img/loadIcon.png",
            path: config.page.invitorShareResult + '?hdCardInitiatorId=' + wx.getStorageSync("initorId"),
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
})